/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.BranchEntity;
import entity.FnBBranchEntity;
import entity.FnBProductEntity;
import entity.FoodTypeEntity;
import entity.MealEntity;
import entity.PriceControlEntity;
import entity.ProductAvailabilityEntity;
import entity.ProductEntity;
import entity.SetMealEntity;
import entity.StakeholderEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import exception.DeleteProductException;

/**
 *
 * @author TrungHieu-Henry
 */
@Stateful
@LocalBean
public class MealSessionBean {

    @PersistenceContext
    private EntityManager entityManager;

    public void addMeal(MealEntity selectedMeal, long typeId, long branchId) {
        FnBBranchEntity branch = entityManager.find(FnBBranchEntity.class, branchId);
        MealEntity meal = new MealEntity();
        meal = selectedMeal;

        entityManager.persist(meal);
        System.out.println("add meal session 4");

        FoodTypeEntity foodType = entityManager.find(FoodTypeEntity.class, typeId);
        meal.setType(foodType);
        meal.setDeposit(branch.getFnBIOTR().getDepositPercentCharge());
        meal.setShowCharge(branch.getFnBIOTR().getShowPercentCharge());
        meal.setNoShowCharge(branch.getFnBIOTR().getNoShowPercentCharge());
        meal.setPointReceived(3);
        foodType.getMeal().add(meal);

        branch.getFandBProduct().add(meal);
        meal.setFnBBranch(branch);
        entityManager.flush();
        System.out.println("add meal session 5");
    }

    public List<ProductEntity> getProductList() {
        Query query = entityManager.createQuery("SELECT p FROM ProductEntity p");
        return query.getResultList();
    }

    public List<FnBProductEntity> getFnBProductListOfABranch(long branchId) {
        FnBBranchEntity branch = entityManager.find(FnBBranchEntity.class, branchId);
        return branch.getFandBProduct();
    }

    public List<MealEntity> getMealListOfABranch(long branchId) {
        List<FnBProductEntity> list = getFnBProductListOfABranch(branchId);
        List<MealEntity> result = new ArrayList<MealEntity>();

        for (FnBProductEntity p : list) {
            if (p instanceof MealEntity) {
                result.add((MealEntity) p);
            }
        }

        return result;
    }

    public List<SetMealEntity> getSetMealListOfABranch(long branchId) {
        List<FnBProductEntity> list = getFnBProductListOfABranch(branchId);
        List<SetMealEntity> result = new ArrayList<SetMealEntity>();

        for (FnBProductEntity p : list) {
            if (p instanceof SetMealEntity) {
                result.add((SetMealEntity) p);
            }
        }

        return result;
    }

    public List<FoodTypeEntity> getFoodType() {
        Query query = entityManager.createQuery("SELECT f FROM FoodTypeEntity f");
        return query.getResultList();
    }

    public void updateMeal(MealEntity meal) {
        entityManager.merge(meal);
    }

    public void updateSetMeal(SetMealEntity selectedSetMeal, MealEntity[] selectedMeals) {
        List<MealEntity> tempList = selectedSetMeal.getMeal();
        MealEntity meal = new MealEntity();
        while (!tempList.isEmpty()) {
            meal = tempList.get(0);
            tempList.remove(0);
            meal.getSetMeal().remove(selectedSetMeal);
            entityManager.merge(meal);
            entityManager.flush();
        }

        for (int i = 0; i < selectedMeals.length; i++) {
            selectedSetMeal.getMeal().add(selectedMeals[i]);
            selectedMeals[i].getSetMeal().add(selectedSetMeal);
            entityManager.merge(selectedMeals[i]);
        }
        entityManager.merge(selectedSetMeal);
    }

    public void addSetMeal(SetMealEntity setMeal, MealEntity[] selectedMeals, long branchId) {
        FnBBranchEntity branch = entityManager.find(FnBBranchEntity.class, branchId);
        entityManager.persist(setMeal);
        for (int i = 0; i < selectedMeals.length; i++) {
            System.out.print(selectedMeals[i].getName());
            setMeal.getMeal().add(selectedMeals[i]);
            selectedMeals[i].getSetMeal().add(setMeal);
            entityManager.merge(selectedMeals[i]);
        }
        setMeal.setDeposit(branch.getFnBIOTR().getDepositPercentCharge());
        setMeal.setShowCharge(branch.getFnBIOTR().getShowPercentCharge());
        setMeal.setNoShowCharge(branch.getFnBIOTR().getNoShowPercentCharge());
        setMeal.setPointReceived(3);
        branch.getFandBProduct().add(setMeal);
        entityManager.merge(setMeal);
        setMeal.setFnBBranch(branch);

        entityManager.flush();
    }

    public void deleteMeal(MealEntity selectedMeal) throws DeleteProductException {
        if (selectedMeal.getReservedMeal().isEmpty() && selectedMeal.getSetMeal().isEmpty()) {

            MealEntity temp = entityManager.find(MealEntity.class, selectedMeal.getId());
            temp.getFnBBranch().getFandBProduct().remove(temp);
            entityManager.merge(temp.getFnBBranch());
            entityManager.refresh(temp);

            deletePriceControlAndAvailability(temp);
            entityManager.remove(temp);
            entityManager.flush();
        } else if (!selectedMeal.getReservedMeal().isEmpty()) {
            throw new DeleteProductException(selectedMeal.getName() + " is still in a set meal ");
        } else {
            throw new DeleteProductException("There is/are customer(s) who reserve " + selectedMeal.getName());
        }
    }

    public void deleteSetMeal(SetMealEntity selectedSetMeal) throws DeleteProductException {
        if (selectedSetMeal.getReservedSetMeal().isEmpty()) {
            SetMealEntity temp = entityManager.find(SetMealEntity.class, selectedSetMeal.getId());

            List<MealEntity> tempList = selectedSetMeal.getMeal();

            //remove all the meals associated with this set meal
            while (!tempList.isEmpty()) {
                MealEntity meal = tempList.get(0);
                tempList.remove(0);
                meal.getSetMeal().remove(temp);
                entityManager.merge(meal);
                entityManager.flush();
            }
            deletePriceControlAndAvailability(temp);

            temp.getFnBBranch().getFandBProduct().remove(temp);
            entityManager.merge(temp.getFnBBranch());

            entityManager.refresh(temp);
            entityManager.remove(temp);
            entityManager.flush();
        } else {
            throw new DeleteProductException("There is/are customer(s) who reserve " + selectedSetMeal.getName());
        }
    }

    private void deletePriceControlAndAvailability(FnBProductEntity temp) {
        //clear the price control list
        List<PriceControlEntity> listOfPrice = temp.getPriceControl();
        while (!listOfPrice.isEmpty()) {
            PriceControlEntity p = listOfPrice.get(0);
            listOfPrice.remove(0);
            entityManager.remove(p);
        }
        entityManager.flush();

        //clear the availability list
        List<ProductAvailabilityEntity> listOfAvail = temp.getProductAvailability();
        for (int i = 0; i < listOfAvail.size(); i++) {
            ProductAvailabilityEntity p = listOfAvail.get(i);
            p.getProduct().remove(temp);
            entityManager.merge(p);
        }
        entityManager.flush();
    }

    public MealEntity[] getMealBySetMealId(long id) {
        SetMealEntity set = entityManager.find(SetMealEntity.class, id);
        List<MealEntity> list = set.getMeal();

        MealEntity[] temp = new MealEntity[list.size()];
        for (int i = 0; i < list.size(); i++) {
            temp[i] = list.get(i);
        }
        return temp;
    }
    //Duy-start

    public List<MealEntity> getMealListOfStakeholder(StakeholderEntity stakeholder) {
        List<BranchEntity> branchList = stakeholder.getBranches();
        List<MealEntity> mealList = new ArrayList<MealEntity>();
        int i;

        for (i = 0; i < branchList.size(); i++) {
            BranchEntity branch = branchList.get(i);
            List<MealEntity> list = getMealListOfABranch(branch.getId());
            for (MealEntity p : list) {
                mealList.add(p);
            }
        }
        return mealList;
    }

    public List<SetMealEntity> getSetMealListOfStakeholder(StakeholderEntity stakeholder) {
        List<BranchEntity> branchList = stakeholder.getBranches();
        List<SetMealEntity> setMealList = new ArrayList<SetMealEntity>();
        int i;

        for (i = 0; i < branchList.size(); i++) {
            BranchEntity branch = branchList.get(i);
            List<SetMealEntity> list = getSetMealListOfABranch(branch.getId());
            for (SetMealEntity p : list) {
                setMealList.add(p);
            }
        }
        return setMealList;
    }
    //Duy-end
}
